package Other;

import java.util.HashMap;
import java.util.Map;

public class leet_1 {


}

class Solution_1 {
    public int[] twoSum(int[] nums, int target) {
        int[] result = doubleFor(nums, target);
        return result;
    }

    // 暴力法解决
    public int[] doubleFor(int[] nums, int target){
        int[] result = new int[2];
        for(int i=0; i<nums.length; i++){
            int value_1 = nums[i];
            for(int j=i+1; j<nums.length; j++){
                int value_2 = nums[j];
                if(value_1 + value_2 == target){
                    result[0] = i;
                    result[1] = j;
                }
            }
        }
        return result;
    }

    // hash映射减少遍历
    public int[] hashFind(int[] nums, int target){
        int[] result = new int[2];
        Map<Integer, Integer> hash = new HashMap<>();
        for(int i=0; i<nums.length; i++){
            hash.put(nums[i], i);
        }
        for(int i=0; i<nums.length; i++){
            int key = target-nums[i];
            if(hash.containsKey(key)){
                int value = hash.get(key);
                if(value==i){
                    // 同一个元素在答案里面不能出现
                    continue;
                }
                result[0] = i;
                result[1] = value;
            }
        }
        return result;
    }

}



